//
//  SN_QuestionCollect_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/26.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_QuestionCollect_Controller.h"
#import "SN_QuestionCollect_TableViewCell.h"
#import "SN_Question_Solve_Model.h"
#import "SN_Question_Detail_Controller.h"
#import "SN_Question_SolveList_Model.h"

@interface SN_QuestionCollect_Controller ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UILabel *emptyLabel;

@property (nonatomic, assign) NSInteger pageNo; //页码
@property (nonatomic, assign) NSInteger pageSize;

@end

@implementation SN_QuestionCollect_Controller

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;

        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        _tableView.rowHeight = 185;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [self requestList:0];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
           
            [self requestMoreList];
        }];
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, 100)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据。";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        _emptyLabel.center = self.view.center;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.navigationItem.title = @"习题收藏";
    
    _pageNo = 1;
    _pageSize = 20;
    
    [self.view addSubview:self.tableView];
    [self requestList:1];
    
    [self.view bringSubviewToFront:self.networkErrorView];
//    [self.tableView.mj_header beginRefreshing];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellid = @"cellid";
    SN_QuestionCollect_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    if (!cell) {
        
        cell = [[SN_QuestionCollect_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        
    }
    
    SN_Question_Solve_Model *model = [self.dataArr objectAtIndex:indexPath.row];
    
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.deleteButton.tag = indexPath.row;
    [cell.deleteButton addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SN_Question_Solve_Model *model = [self.dataArr objectAtIndex:indexPath.row];
    
    //先请求该期报纸的所有习题
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"api_szb_questionlist" forKey:@"Method"];
    [dict setObject:model.newid forKey:@"newsid"];
    [dict setObject:USERID forKey:@"userid"];

    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSDictionary *dict = [arr objectAtIndex:0];

        NSArray *questionListArr = [dict objectForKey:@"bigquestionlist"];
        
        
        NSMutableArray *arrM = [SN_Question_SolveList_Model mj_objectArrayWithKeyValuesArray:questionListArr];
        
        
        SN_Question_Detail_Controller *vc = [[SN_Question_Detail_Controller alloc] init];
        
        for (NSInteger i = 0; i < arrM.count; i++) {
            
            SN_Question_SolveList_Model *listModel = [arrM objectAtIndex:i];
            
            for (NSInteger j = 0; j < listModel.questionlist.count; j++) {
                
                SN_Question_Solve_Model *solveModel = [listModel.questionlist objectAtIndex:j];
                
                if ([solveModel.problemid isEqualToString:model.problemid]) {
                    
                    vc.selectIndex = j;
                    vc.listModel = listModel;
                    vc.navTitle = [NSString stringWithFormat:@"%ld期 %@",(long)model.phase,model.newname];
                    
                    
                    vc.dataArr = arrM;
                    vc.collectionBlock = ^(){
                    
                        [self requestList:0];
                        
                    };
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
            
        }
        
        
    } failure:^(NSInteger recode, NSError *error) {
       
        [self showHint:error.localizedDescription];
    }];

}


#pragma mark -Request
- (void)requestList:(int)type {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_szb_problemcollectionlist" forKey:@"Method"];
    [param setObject:USERID forKey:@"userid"];
    
    _pageNo = 1;
    [param setObject:@(_pageNo) forKey:@"pageno"];
    
    [param setObject:@(_pageSize) forKey:@"pagesize"];
    
    [Utils getWithparameters:param showHUD:NO success:^(NSInteger recode, id jsonObject) {
        [self.dataArr removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        if (arr.count == 0) {
            
            self.emptyLabel.hidden = NO;
            [self.tableView reloadData];
            return;
        }
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            _pageNo++;
        }
        
        self.dataArr = [SN_Question_Solve_Model mj_objectArrayWithKeyValuesArray:arr];
        self.emptyLabel.hidden = YES;
        [self.tableView reloadData];
        
        [self.networkErrorView hideEmptyImage];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self showHint:error.localizedDescription];
        
        if (type == 1) {
            
            [self.networkErrorView showEmptyImage];
        }
    }];
}

//请求更多
- (void)requestMoreList {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_szb_problemcollectionlist" forKey:@"Method"];
    [param setObject:USERID forKey:@"userid"];
    
    [param setObject:@(_pageNo) forKey:@"pageno"];
    
    [param setObject:@(_pageSize) forKey:@"pagesize"];
    
    [Utils getWithparameters:param showHUD:NO success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
    
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            [self.tableView.mj_footer endRefreshing];
            _pageNo++;
        }
        
        NSArray *modelArr = [SN_Question_Solve_Model mj_objectArrayWithKeyValuesArray:arr];
        
        [self.dataArr addObjectsFromArray:modelArr];
        
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self showHint:error.localizedDescription];
    }];
}

//删除习题
- (void)deleteAction:(UIButton *)btn {
    
    NSInteger index = btn.tag;
    
    SN_Question_Solve_Model *model = [self.dataArr objectAtIndex:index];
    
    //删除试题
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_szb_setproblemcollection" forKey:@"method"];
    [param setObject:model.problemid forKey:@"problemid"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"0" forKey:@"1"];
    
    [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSString *re_info = [jsonObject objectForKey:@"re_info"];
        [self showHint:re_info];
        
        [self.dataArr removeObjectAtIndex:index];
        [self.tableView reloadData];
        
        if (self.dataArr.count == 0) {
            self.emptyLabel.hidden = NO;
        }else {
            self.emptyLabel.hidden = YES;
        }
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
}

- (void)reloadCurrentData {

    [self requestList:1];
}


@end
